using Microsoft.EntityFrameworkCore;
using DaoHang.Data;
using DaoHang.Models;

namespace DaoHang.Services
{
    /// <summary>
    /// 数据初始化服务实现类，负责初始化系统默认数据
    /// </summary>
    public class DataSeedService : IDataSeedService
    {
        private readonly ApplicationDbContext context;
        private readonly ILogService logService;
        private readonly ILogger<DataSeedService> logger;
        private readonly IIconService iconService;

        /// <summary>
        /// 构造函数，注入依赖服务
        /// </summary>
        /// <param name="context">数据库上下文</param>
        /// <param name="logService">日志服务</param>
        /// <param name="logger">日志记录器</param>
        /// <param name="iconService">图标服务</param>
        public DataSeedService(ApplicationDbContext context, ILogService logService, ILogger<DataSeedService> logger, IIconService iconService)
        {
            this.context = context;
            this.logService = logService;
            this.logger = logger;
            this.iconService = iconService;
        }

        /// <summary>
        /// 检查是否需要初始化默认数据
        /// </summary>
        /// <returns>是否需要初始化</returns>
        public async Task<bool> NeedsSeedDataAsync()
        {
            try
            {
                // 检查是否已有网站数据
                var websiteCount = await context.Websites.CountAsync();
                var categoryCount = await context.Categories.CountAsync();
                
                // 如果没有任何网站数据，则需要初始化
                return websiteCount == 0 && categoryCount == 0;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "检查是否需要初始化数据时发生错误");
                return false;
            }
        }

        /// <summary>
        /// 初始化默认分类数据
        /// </summary>
        /// <returns>初始化结果</returns>
        public async Task<bool> SeedDefaultCategoriesAsync()
        {
            try
            {
                // 检查是否已有分类数据
                var existingCategories = await context.Categories.CountAsync();
                if (existingCategories > 0)
                {
                    await logService.LogInformationAsync("分类数据已存在，跳过初始化", "DataSeed");
                    return true;
                }

                // 只保留编程工具相关的分类，并新增AI分类
                var defaultCategories = new List<Category>
                {
                    new Category
                    {
                        Name = "开发工具",
                        Description = "程序开发相关工具和平台",
                        SortOrder = 1,
                        CreateTime = DateTime.Now
                    },
                    new Category
                    {
                        Name = "在线工具",
                        Description = "实用的在线编程工具和服务",
                        SortOrder = 2,
                        CreateTime = DateTime.Now
                    },
                    new Category
                    {
                        Name = "学习资源",
                        Description = "编程学习和技术文档平台",
                        SortOrder = 3,
                        CreateTime = DateTime.Now
                    },
                    new Category
                    {
                        Name = "云服务",
                        Description = "云计算和部署平台",
                        SortOrder = 4,
                        CreateTime = DateTime.Now
                    },
                    new Category
                    {
                        Name = "AI工具",
                        Description = "人工智能和大模型相关平台",
                        SortOrder = 5,
                        CreateTime = DateTime.Now
                    }
                };

                context.Categories.AddRange(defaultCategories);
                await context.SaveChangesAsync();

                await logService.LogInformationAsync($"成功初始化 {defaultCategories.Count} 个默认分类", "DataSeed");
                return true;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "初始化默认分类数据时发生错误");
                await logService.LogErrorAsync("初始化默认分类失败", ex, "DataSeed");
                return false;
            }
        }

        /// <summary>
        /// 初始化默认网站数据
        /// </summary>
        /// <returns>初始化结果</returns>
        public async Task<bool> SeedDefaultWebsitesAsync()
        {
            try
            {
                // 检查是否已有网站数据
                var existingWebsites = await context.Websites.CountAsync();
                if (existingWebsites > 0)
                {
                    await logService.LogInformationAsync("网站数据已存在，跳过初始化", "DataSeed");
                    return true;
                }

                // 获取已创建的分类
                var categories = await context.Categories.ToListAsync();
                if (!categories.Any())
                {
                    await logService.LogWarningAsync("未找到分类数据，无法初始化网站", "DataSeed");
                    return false;
                }

                // 创建分类名称到ID的映射
                var categoryMap = categories.ToDictionary(c => c.Name, c => c.Id);

                await logService.LogInformationAsync("开始初始化默认网站数据，将自动下载图标到本地", "DataSeed");

                // 只保留编程工具相关的网站，移除IconUrl让系统自动下载
                var defaultWebsites = new List<Website>
                {
                    // 开发工具
                    new Website
                    {
                        Name = "GitHub",
                        Url = "https://github.com",
                        Description = "全球最大的代码托管平台",
                        CategoryId = categoryMap.GetValueOrDefault("开发工具", 1),
                        SortOrder = 1,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Gitee",
                        Url = "https://gitee.com",
                        Description = "国内领先的代码托管平台",
                        CategoryId = categoryMap.GetValueOrDefault("开发工具", 1),
                        SortOrder = 2,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Visual Studio Code",
                        Url = "https://code.visualstudio.com",
                        Description = "微软开源的强大代码编辑器",
                        CategoryId = categoryMap.GetValueOrDefault("开发工具", 1),
                        SortOrder = 3,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "JetBrains",
                        Url = "https://www.jetbrains.com",
                        Description = "专业开发工具套件",
                        CategoryId = categoryMap.GetValueOrDefault("开发工具", 1),
                        SortOrder = 4,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Docker",
                        Url = "https://www.docker.com",
                        Description = "容器化应用开发平台",
                        CategoryId = categoryMap.GetValueOrDefault("开发工具", 1),
                        SortOrder = 5,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Postman",
                        Url = "https://www.postman.com",
                        Description = "API开发和测试工具",
                        CategoryId = categoryMap.GetValueOrDefault("开发工具", 1),
                        SortOrder = 6,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },

                    // 在线工具
                    new Website
                    {
                        Name = "CodePen",
                        Url = "https://codepen.io",
                        Description = "在线代码编辑和分享平台",
                        CategoryId = categoryMap.GetValueOrDefault("在线工具", 2),
                        SortOrder = 1,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "JSFiddle",
                        Url = "https://jsfiddle.net",
                        Description = "在线JavaScript代码测试工具",
                        CategoryId = categoryMap.GetValueOrDefault("在线工具", 2),
                        SortOrder = 2,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Can I use",
                        Url = "https://caniuse.com",
                        Description = "查询Web前端技术浏览器兼容性",
                        CategoryId = categoryMap.GetValueOrDefault("在线工具", 2),
                        SortOrder = 3,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "JSON在线解析",
                        Url = "https://www.json.cn",
                        Description = "JSON在线解析及格式化验证",
                        CategoryId = categoryMap.GetValueOrDefault("在线工具", 2),
                        SortOrder = 4,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "正则表达式测试",
                        Url = "https://regex101.com",
                        Description = "在线正则表达式测试和学习工具",
                        CategoryId = categoryMap.GetValueOrDefault("在线工具", 2),
                        SortOrder = 5,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "ProcessOn",
                        Url = "https://www.processon.com",
                        Description = "在线流程图、思维导图、UML等",
                        CategoryId = categoryMap.GetValueOrDefault("在线工具", 2),
                        SortOrder = 6,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },

                    // 学习资源
                    new Website
                    {
                        Name = "Stack Overflow",
                        Url = "https://stackoverflow.com",
                        Description = "全球最大的程序员问答社区",
                        CategoryId = categoryMap.GetValueOrDefault("学习资源", 3),
                        SortOrder = 1,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "MDN Web Docs",
                        Url = "https://developer.mozilla.org",
                        Description = "Web技术权威文档",
                        CategoryId = categoryMap.GetValueOrDefault("学习资源", 3),
                        SortOrder = 2,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "LeetCode",
                        Url = "https://leetcode.com",
                        Description = "程序员刷题平台",
                        CategoryId = categoryMap.GetValueOrDefault("学习资源", 3),
                        SortOrder = 3,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "菜鸟教程",
                        Url = "https://www.runoob.com",
                        Description = "编程语言和开发工具的中文教程",
                        CategoryId = categoryMap.GetValueOrDefault("学习资源", 3),
                        SortOrder = 4,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "freeCodeCamp",
                        Url = "https://www.freecodecamp.org",
                        Description = "免费编程学习平台",
                        CategoryId = categoryMap.GetValueOrDefault("学习资源", 3),
                        SortOrder = 5,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "W3Schools",
                        Url = "https://www.w3schools.com",
                        Description = "Web技术学习教程",
                        CategoryId = categoryMap.GetValueOrDefault("学习资源", 3),
                        SortOrder = 6,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },

                    // 云服务
                    new Website
                    {
                        Name = "阿里云",
                        Url = "https://www.aliyun.com",
                        Description = "阿里巴巴旗下云服务平台",
                        CategoryId = categoryMap.GetValueOrDefault("云服务", 4),
                        SortOrder = 1,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "腾讯云",
                        Url = "https://cloud.tencent.com",
                        Description = "腾讯旗下云服务平台",
                        CategoryId = categoryMap.GetValueOrDefault("云服务", 4),
                        SortOrder = 2,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "AWS",
                        Url = "https://aws.amazon.com",
                        Description = "亚马逊云服务平台",
                        CategoryId = categoryMap.GetValueOrDefault("云服务", 4),
                        SortOrder = 3,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Vercel",
                        Url = "https://vercel.com",
                        Description = "前端应用部署平台",
                        CategoryId = categoryMap.GetValueOrDefault("云服务", 4),
                        SortOrder = 4,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Netlify",
                        Url = "https://www.netlify.com",
                        Description = "静态网站托管和部署平台",
                        CategoryId = categoryMap.GetValueOrDefault("云服务", 4),
                        SortOrder = 5,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Heroku",
                        Url = "https://www.heroku.com",
                        Description = "云应用平台",
                        CategoryId = categoryMap.GetValueOrDefault("云服务", 4),
                        SortOrder = 6,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },

                    // AI工具
                    new Website
                    {
                        Name = "文心一言",
                        Url = "https://yiyan.baidu.com",
                        Description = "百度推出的知识增强大语言模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 1,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "通义千问",
                        Url = "https://tongyi.aliyun.com",
                        Description = "阿里云推出的超大规模语言模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 2,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "讯飞星火",
                        Url = "https://xinghuo.xfyun.cn",
                        Description = "科大讯飞推出的新一代认知智能大模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 3,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "智谱清言",
                        Url = "https://chatglm.cn",
                        Description = "智谱AI推出的对话式AI助手",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 4,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "腾讯混元",
                        Url = "https://hunyuan.tencent.com",
                        Description = "腾讯推出的大语言模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 5,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "豆包",
                        Url = "https://www.doubao.com",
                        Description = "字节跳动推出的AI助手",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 6,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Kimi Chat",
                        Url = "https://kimi.moonshot.cn",
                        Description = "月之暗面推出的智能助手",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 7,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "商汤商量",
                        Url = "https://chat.sensetime.com",
                        Description = "商汤科技推出的大语言模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 8,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "360智脑",
                        Url = "https://ai.360.cn",
                        Description = "360推出的大语言模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 9,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Claude",
                        Url = "https://claude.ai",
                        Description = "Anthropic推出的AI助手",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 10,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "OpenAI",
                        Url = "https://openai.com",
                        Description = "OpenAI官方网站和API服务",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 11,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Hugging Face",
                        Url = "https://huggingface.co",
                        Description = "开源AI模型和数据集平台",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 12,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Midjourney",
                        Url = "https://www.midjourney.com",
                        Description = "AI图像生成工具",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 13,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Stable Diffusion",
                        Url = "https://stability.ai",
                        Description = "开源AI图像生成模型",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 14,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Replicate",
                        Url = "https://replicate.com",
                        Description = "运行开源机器学习模型的平台",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 15,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    },
                    new Website
                    {
                        Name = "Cohere",
                        Url = "https://cohere.ai",
                        Description = "企业级AI语言模型平台",
                        CategoryId = categoryMap.GetValueOrDefault("AI工具", 5),
                        SortOrder = 16,
                        VisitCount = 0,
                        CreateTime = DateTime.Now
                    }
                };

                // 首先保存所有网站到数据库
                context.Websites.AddRange(defaultWebsites);
                await context.SaveChangesAsync();

                await logService.LogInformationAsync($"已保存 {defaultWebsites.Count} 个默认网站到数据库", "DataSeed");

                // 然后为每个网站下载图标到本地
                int successCount = 0;
                int failureCount = 0;

                foreach (var website in defaultWebsites)
                {
                    try
                    {
                        logger.LogInformation("正在为网站 {Name} 下载图标", website.Name);
                        
                        var localIconPath = await iconService.DetectAndDownloadIconAsync(website.Url, website.Name);
                        
                        // 更新数据库中的图标路径
                        website.IconUrl = localIconPath;
                        context.Websites.Update(website);
                        
                        successCount++;
                        logger.LogInformation("网站 {Name} 图标下载成功: {IconPath}", website.Name, localIconPath);
                    }
                    catch (Exception ex)
                    {
                        failureCount++;
                        logger.LogWarning(ex, "网站 {Name} 图标下载失败", website.Name);
                        
                        // 如果下载失败，设置为默认图标
                        website.IconUrl = iconService.GetDefaultIconPath();
                        context.Websites.Update(website);
                    }
                }

                // 保存图标路径更新
                await context.SaveChangesAsync();

                await logService.LogInformationAsync($"默认网站图标下载完成: 成功 {successCount} 个，失败 {failureCount} 个", "DataSeed");
                await logService.LogInformationAsync($"成功初始化 {defaultWebsites.Count} 个默认网站", "DataSeed");
                
                return true;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "初始化默认网站数据时发生错误");
                await logService.LogErrorAsync("初始化默认网站失败", ex, "DataSeed");
                return false;
            }
        }

        /// <summary>
        /// 执行完整的数据初始化
        /// </summary>
        /// <returns>初始化结果</returns>
        public async Task<bool> SeedAllDefaultDataAsync()
        {
            try
            {
                bool needsSeeding = await NeedsSeedDataAsync();
                
                if (needsSeeding)
                {
                    await logService.LogInformationAsync("开始执行完整的数据初始化", "DataSeed");
                    
                    // 先初始化分类
                    var categoriesResult = await SeedDefaultCategoriesAsync();
                    if (!categoriesResult)
                    {
                        await logService.LogErrorAsync("分类初始化失败，终止数据初始化", null, "DataSeed");
                        return false;
                    }
                    
                    // 再初始化网站
                    var websitesResult = await SeedDefaultWebsitesAsync();
                    if (!websitesResult)
                    {
                        await logService.LogErrorAsync("网站初始化失败，终止数据初始化", null, "DataSeed");
                        return false;
                    }
                    
                    await logService.LogInformationAsync("完整的数据初始化执行成功", "DataSeed");
                    return true;
                }
                else
                {
                    await logService.LogInformationAsync("数据已存在，跳过初始化", "DataSeed");
                    return true;
                }
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "执行完整数据初始化时发生错误");
                await logService.LogErrorAsync("完整数据初始化失败", ex, "DataSeed");
                return false;
            }
        }

        /// <summary>
        /// 更新现有默认网站的图标到本地存储
        /// 用于解决已有系统中默认网站仍使用外部图标链接的问题
        /// </summary>
        /// <returns>更新结果</returns>
        public async Task<bool> UpdateDefaultWebsiteIconsAsync()
        {
            try
            {
                await logService.LogInformationAsync("开始更新现有默认网站图标到本地存储", "DataSeed");

                // 获取所有现有网站
                var existingWebsites = await context.Websites.ToListAsync();
                if (!existingWebsites.Any())
                {
                    await logService.LogInformationAsync("未找到现有网站，无需更新图标", "DataSeed");
                    return true;
                }

                // 定义需要更新的默认网站URL列表（这些是我们知道的默认网站）
                var defaultWebsiteUrls = new HashSet<string>
                {
                    // 开发工具
                    "https://github.com",
                    "https://gitee.com", 
                    "https://code.visualstudio.com",
                    "https://www.jetbrains.com",
                    "https://www.docker.com",
                    "https://www.postman.com",
                    
                    // 在线工具
                    "https://codepen.io",
                    "https://jsfiddle.net",
                    "https://caniuse.com",
                    "https://www.json.cn",
                    "https://regex101.com",
                    "https://www.processon.com",
                    
                    // 学习资源
                    "https://stackoverflow.com",
                    "https://developer.mozilla.org",
                    "https://leetcode.com",
                    "https://www.runoob.com",
                    "https://www.freecodecamp.org",
                    "https://www.w3schools.com",
                    
                    // 云服务
                    "https://www.aliyun.com",
                    "https://cloud.tencent.com",
                    "https://aws.amazon.com",
                    "https://vercel.com",
                    "https://www.netlify.com",
                    "https://www.heroku.com",
                    
                    // AI工具
                    "https://yiyan.baidu.com",
                    "https://tongyi.aliyun.com",
                    "https://xinghuo.xfyun.cn",
                    "https://chatglm.cn",
                    "https://hunyuan.tencent.com",
                    "https://www.doubao.com",
                    "https://kimi.moonshot.cn",
                    "https://chat.sensetime.com",
                    "https://ai.360.cn",
                    "https://claude.ai",
                    "https://openai.com",
                    "https://huggingface.co",
                    "https://www.midjourney.com",
                    "https://stability.ai",
                    "https://replicate.com",
                    "https://cohere.ai"
                };

                // 筛选出需要更新的默认网站（URL匹配且图标是外部链接）
                var websitesToUpdate = existingWebsites.Where(w => 
                    defaultWebsiteUrls.Contains(w.Url) && 
                    (string.IsNullOrEmpty(w.IconUrl) || 
                     w.IconUrl.StartsWith("http://") || 
                     w.IconUrl.StartsWith("https://"))).ToList();

                if (!websitesToUpdate.Any())
                {
                    await logService.LogInformationAsync("未找到需要更新图标的默认网站", "DataSeed");
                    return true;
                }

                await logService.LogInformationAsync($"找到 {websitesToUpdate.Count} 个需要更新图标的默认网站", "DataSeed");

                int successCount = 0;
                int failureCount = 0;

                // 逐个更新网站图标
                foreach (var website in websitesToUpdate)
                {
                    try
                    {
                        logger.LogInformation("正在更新网站 {Name} 的图标", website.Name);
                        
                        var localIconPath = await iconService.DetectAndDownloadIconAsync(website.Url, website.Name);
                        
                        // 更新数据库中的图标路径
                        website.IconUrl = localIconPath;
                        context.Websites.Update(website);
                        
                        successCount++;
                        logger.LogInformation("网站 {Name} 图标更新成功: {IconPath}", website.Name, localIconPath);
                    }
                    catch (Exception ex)
                    {
                        failureCount++;
                        logger.LogWarning(ex, "网站 {Name} 图标更新失败", website.Name);
                        
                        // 如果下载失败，设置为默认图标
                        website.IconUrl = iconService.GetDefaultIconPath();
                        context.Websites.Update(website);
                    }
                }

                // 保存所有更新
                await context.SaveChangesAsync();

                await logService.LogInformationAsync($"默认网站图标更新完成: 成功 {successCount} 个，失败 {failureCount} 个", "DataSeed");
                
                return true;
            }
            catch (Exception ex)
            {
                logger.LogError(ex, "更新默认网站图标时发生错误");
                await logService.LogErrorAsync("更新默认网站图标失败", ex, "DataSeed");
                return false;
            }
        }
    }
} 